#include "client.h"

#include <anet/anet.h>
#include <anet/ae.h>
#include <anet/util.h>

#include "buffer.h"
#include "role.h"

Client* ClientCreate(struct aeEventLoop* el, int fd, void* data) {
    Client* client = (Client *)malloc(sizeof(Client));
    client->el = el;
    client->fd = fd;
    client->data = data;

    client->input = BufferCreate(128);
    client->output = BufferCreate(128);

    LogInfo("client create %p", client);

    return client;
}

void ClientDestroy(Client* client) {
  if (client) {
    LogInfo("client destroy %p", client);

    if (client->input) {
      BufferDestroy(client->input);
    }
    if (client->output) {
      BufferDestroy(client->output);
    }
    anetClose(client->fd);
    if (client->data) {
      Role* role = (Role *)client->data;
      RoleDestroy(role);
    }
    free(client);
  }
}
